package com.isunland.managesystem.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import com.google.gson.Gson;
import com.isunland.managesystem.adapter.CustomerProblemReplyListAdapter;
import com.isunland.managesystem.base.Base;
import com.isunland.managesystem.base.BaseListFragment;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.entity.CustomerProblemReply;
import com.isunland.managesystem.entity.CustomerProblemReplyOriginal;
import com.isunland.managesystem.utils.ToastUtil;
import java.util.ArrayList;
import java.util.HashMap;

/**
 * Created by Lenovo on 2017/3/1.
 */
public class CustomerProblemReplyListFragment extends BaseListFragment {
  protected static final String EXTRA_CONTENT =
      "com.isunland.managesystem.ui.CustomerProblemReplyListFragment.EXTRA_CONTENT";

  private ArrayList<CustomerProblemReply> mList;
  private String mainId;

  @Override public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mainId = getArguments().getString(EXTRA_CONTENT);
    mList = new ArrayList<>();
  }

  @Override public String getUrl() {
    return ApiConst.URL_PROBLEM_REPLY;
  }

  @Override public HashMap<String, String> getparameters() {
    HashMap<String, String> params = new HashMap<>();
    params.put("refid", mainId);
    return params;
  }

  @Override public void resolveResponse(String json) {
    CustomerProblemReplyOriginal result =
        new Gson().fromJson(json, CustomerProblemReplyOriginal.class);
    if (result.getResult() == Base.SUCCESS && result.getRows() != null) {
      //获取列表成功
      mList.clear();
      mList.addAll(result.getRows());
      CustomerProblemReplyListAdapter mAdapter =
          new CustomerProblemReplyListAdapter(getActivity(), mList);
      setListAdapter(mAdapter);
    } else {
      ToastUtil.show("获取列表失败");
    }
  }

  @Override public void onListItemClick(ListView l, View v, int position, long id) {
    CustomerProblemReply content = mList.get(position - 1);
    Intent i = new Intent(getActivity(), CustomerProblemReplyActivity.class);
    i.putExtra(CustomerProblemReplyFragment.EXTRA_CONTENT, content);
    startActivity(i);
  }

  public static CustomerProblemReplyListFragment newInstance(String mainId) {

    Bundle args = new Bundle();
    args.putString(EXTRA_CONTENT, mainId);
    CustomerProblemReplyListFragment fragment = new CustomerProblemReplyListFragment();
    fragment.setArguments(args);
    return fragment;
  }
}
